#include <unistd.h> 
#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <sys/epoll.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/time.h>
#include <pthread.h>
#include <stdarg.h>
#include <time.h>


int	nAgentType;
int	nAgentOp;
int	nFwFileSize;
int	nAmssFileSize;
int	getHeader(char *szFn);

int main(int ac, char *av[])
{
	if ( ac != 2 ) {
		printf("Usage %s fwfile\n", av[0]);
		exit(0);
	}
	getHeader(av[1]);

	return 0;
}

int getHeader(char *szFn)
{
    FILE    *fp2, *fp1;
    char    szHeaderFname[128];
    char    szRsrv1[8];
    char    szRsrv2[8];
    char    szBodyFname[128];
    int     nHeaderFileSize, nAmssCheckSum, nThr=0;
    int     nRsrv1, nRsrv2;

    memset(szBodyFname, 0x00, sizeof(szBodyFname));
    memset(szHeaderFname, 0x00, sizeof(szHeaderFname));

    // Load Header file
    if ( strstr(szFn, ".") == NULL ) {
        sprintf(szHeaderFname, "/opt/fota/data/FwUpgrade/%s.noagent.header", szFn);
        sprintf(szBodyFname, "/opt/fota/data/FwUpgrade/%s.noagent.gz", szFn);
    } else {
        sprintf(szHeaderFname, "/opt/fota/data/FwUpgrade/%s.delta.header", szFn);
        sprintf(szBodyFname, "/opt/fota/data/FwUpgrade/%s.delta.gz", szFn);
    }

    /* -----------
        Header
     ----------- */
    if( (fp1=fopen(szHeaderFname,"r"))==NULL) {
        printf( (char *)"[%4d] Can't open Header file(%s)\n", nThr, szHeaderFname);
        return 1;
    }

    fseek(fp1,0,SEEK_END);
    nHeaderFileSize  = ftell(fp1);
    rewind(fp1);

    char    szHeaderData[nHeaderFileSize+4];
    memset(szHeaderData, 0x00, sizeof(szHeaderData));

    printf((char *)"[%4d] Load Header file(%s) size(%d)\n", nThr, szHeaderFname, nHeaderFileSize);

    int len = fread(szHeaderData, 1, nHeaderFileSize, fp1);
    if( len != nHeaderFileSize) {
        printf( (char *)"[%4d] FirmWare Header mismatch ! %d, %d\n", nThr, len, nHeaderFileSize);
        fclose(fp1);
        return 1;
    }
    nAgentType = szHeaderData[0];
    nAgentOp = szHeaderData[1];
    memcpy(&nFwFileSize, &szHeaderData[2], sizeof(int));
    memcpy(&nAmssFileSize, &szHeaderData[6], sizeof(int));

    if ( nHeaderFileSize > 10 ) {
        memcpy(&nAmssCheckSum, &szHeaderData[10], sizeof(int));
        nAmssCheckSum = nAmssCheckSum;
        printf((char *)"[%4d] VER1.2 Header AmssCheckSum(%d)\n", nThr, nAmssCheckSum);
#if	0
        memcpy(szRsrv1, &szHeaderData[14], sizeof(int));
        memcpy(szRsrv2, &szHeaderData[18], sizeof(int));
#else
        memcpy(&nRsrv1, &szHeaderData[14], sizeof(int));
        memcpy(&nRsrv2, &szHeaderData[18], sizeof(int));
#endif
    }
    fclose(fp1);

	printf("AgentType (%d)\n", nAgentType);
	printf("AgentOp (%d)\n", nAgentOp);
	printf("nFwFileSize (%d)\n", nFwFileSize);
	printf("nAmssFileSize (%d)\n", nAmssFileSize);
	printf("nAmssCheckSum (%d)\n", nAmssCheckSum);
#if	0	
	printf("szRsrv1 (%s)\n", szRsrv1);
	printf("szRsrv2 (%s)\n", szRsrv2);
#else
	printf("nRsrv1 (%d)\n", nRsrv1);
	printf("nRsrv2 (%d)\n", nRsrv2);
#endif

	return 0;
}
